﻿@page "/useraccounts/create"

@using Microsoft.EntityFrameworkCore
@using BlazorAppAuth.Models
@inject IDbContextFactory<BlazorAppAuth.Data.BlazorAppAuthContext> DbFactory
@inject NavigationManager NavigationManager


<EditForm method="post" Model="UserAccount" OnValidSubmit="AddUserAccount" FormName="create" Enhance>
	<DataAnnotationsValidator />
	<ValidationSummary class="text-danger" />
	<div class="mb-3">
		<label for="name" class="form-label">Name:</label>
		<InputText id="name" @bind-Value="UserAccount.Name" class="form-control" />
		<ValidationMessage For="() => UserAccount.Name" class="text-danger" />
	</div>
	<div class="mb-3">
		<label for="password" class="form-label">Password:</label>
		<InputText id="password" @bind-Value="UserAccount.Password" type="password" class="form-control" />
		<ValidationMessage For="() => UserAccount.Password" class="text-danger" />
	</div>
	<div class="mb-3">
		<label for="role" class="form-label">Role:</label>
		<InputSelect id="role" @bind-Value="UserAccount.Role" class="form-control">
			@foreach (var role in UserRoles)
			{
				<option value="@role">@role</option>
			}
		</InputSelect>
		<ValidationMessage For="() => UserAccount.Role" class="text-danger" />
	</div>
	<button type="submit" class="btn btn-primary">创建</button>
</EditForm>



@code {

	[SupplyParameterFromForm]
	public UserAccount UserAccount { get; set; } = new();

	[Parameter]
	public EventCallback OnCreated { get; set; }

	private List<string> UserRoles = new List<string> { "超级管理员", "管理员", "维修工", "操作员" };

	// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
	public async Task AddUserAccount()
	{
		using var context = DbFactory.CreateDbContext();
		context.UserAccount.Add(UserAccount);
		await context.SaveChangesAsync();
		await OnCreated.InvokeAsync();
	}
}
